﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GUIServerConnectorAPI;
using GUI;
using EntityModel;

namespace LoginForm
{
    public partial class SupraveghetorForm : Form
    {
        private LoginForm loginForm;
        private Guid sessionId;
     //   private string idPacientVar;

        public SupraveghetorForm(LoginForm loginForm)
        {
            InitializeComponent();
            this.loginForm = loginForm;
        }

        public SupraveghetorForm()
        {
            InitializeComponent();
        }
        public void Show(Guid sessionId)
        {
            this.sessionId = sessionId;
            Show();
        }

        private void SupraveghetorForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GUIServerConnector.getInstance().getAuthenticator().logout(sessionId);
            this.Hide();
            loginForm.Reset(this);
        }

        private void SupraveghetorForm_Load(object sender, EventArgs e)
        {
          //  pictureBox1.Image = GUI.Properties.Resources.alarmaOK;
          //  pictureBox2.Image = GUI.Properties.Resources.alarmaNOTOK;

            List<Pacient> listaPacienti = GUIServerConnector.getInstance().getServerInterface().getPacientsData(sessionId);
            listView.BeginUpdate();
            foreach (Pacient pacient in listaPacienti)
            {
                ListViewItem item = new ListViewItem(pacient.Nume.ToString()+" "+pacient.Prenume.ToString());
                listView.Items.Add(item);
            }
            listView.EndUpdate();


            
        }

        public void LoadPacientData(Pacient pacient)
        {
           // Pacient pacient = GUIServerConnector.getInstance().getServerInterface().getPacientData(sessionId);
            NumeLabel.Text = "Nume: " + pacient.Nume.ToString();
            PrenumeLabel.Text = "Prenume: " + pacient.Prenume.ToString();
            SexLabel.Text = "Sex: " + pacient.Sex.ToString();
            DataNasteriiLabel.Text = "Data Nasterii: " + pacient.Data_nasterii.ToShortDateString();
            CNPLabel.Text = "CNP: " + pacient.CNP.ToString();
            groupBox8.Text = "Senzori Pacient:[ " + pacient.Nume.ToString() + " " + pacient.Prenume.ToString() + " ]";

          //  idPacientVar = pacient.idPacient.ToString();
            

        }

        public void LoadTratamentData(Pacient pacient)
        {
            List<Tratamente> listaTratatmente = GUIServerConnector.getInstance().getServerInterface().getTreatments(sessionId, pacient.idPacient);
            foreach (Tratamente tratatment in listaTratatmente)
            {
                /*
                if (tratatment.idTratament.Equals(pacient.idTratament))
                {
                    Medicamentelabel.Text = "Medicamente:" + tratatment.Medicamente.ToString();
                    CantitateLabel.Text = "Cantitate:" + tratatment.Cantitate.ToString();
                    Duratalabel.Text = "Durata:" + tratatment.Durata_tratament.ToString();
                    Diagnosticlabel.Text = "Diagnostic:" + tratatment.Diagnostic.ToString();
                    ObservatiiLabel.Text = "Observatii:" + tratatment.Observatii.ToString();
                    IntervalLabel.Text = "Interval:" + tratatment.Interval.ToString();
                }
                */
            }
        }

        public bool CompareAlarmValue(DefiniteAlarme defAlarme,ValoriAlarme valAlarme)
        {
            bool CompareVar;

            if ((valAlarme.Valoare > defAlarme.ValoarePragInferior) && (valAlarme.Valoare < defAlarme.ValoarePragSuperior))
                CompareVar = true;

            else CompareVar = false;

            return CompareVar;
          
        
        }
        public void LoadAlarmData(Pacient pacient)
        {
            List<DefiniteAlarme> ListadefAlarme = GUIServerConnector.getInstance().getServerInterface().getAlarmDefinitions(sessionId);
            List<ValoriAlarme> valoriAlarme = GUIServerConnector.getInstance().getServerInterface().getAlarms(sessionId);
            foreach (DefiniteAlarme defalarma in ListadefAlarme)
            {
                foreach (ValoriAlarme valalarma in valoriAlarme)
                { 
                 if(defalarma.idDefinitieAlarme.Equals(valalarma.idDefinitieAlarma))
                 {
                     if (CompareAlarmValue(defalarma, valalarma))
                     {
                         if (defalarma.DenumireAlarma == "Temperatura")

                             pictureBoxx1.Image = GUI.Properties.Resources.alarmaOK;
                         else if (defalarma.DenumireAlarma == "Presiune")
                             pictureBox2.Image = GUI.Properties.Resources.alarmaOK;
                         else if (defalarma.DenumireAlarma == "Lumina")
                             pictureBox3.Image = GUI.Properties.Resources.alarmaOK;
                         //restul de else if cand se vor sti exact toti senzorii
                     }
                     else 
                     {
                         if (defalarma.DenumireAlarma == "Temperatura")

                             pictureBoxx1.Image = GUI.Properties.Resources.alarmaNOTOK;
                         else if (defalarma.DenumireAlarma == "Presiune")
                             pictureBox2.Image = GUI.Properties.Resources.alarmaNOTOK;
                         else if (defalarma.DenumireAlarma == "Lumina")
                             pictureBox3.Image = GUI.Properties.Resources.alarmaNOTOK;
                         //restul de else if cand se vor sti exact toti senzorii
                     }
                 }
                }
            }

        }
        public void LoadSenzorsData()
        {
            ColumnHeader colHead;
            List<DefinitieSenzori> defSenzori = GUIServerConnector.getInstance().getServerInterface().getSensorDefinitions(sessionId);

            foreach (DefinitieSenzori senzor in defSenzori)
            {
                colHead = new ColumnHeader();
                colHead.Text = senzor.Tip_Senzori.ToString();
                this.listView.Columns.Add(colHead);
            }

            List<ValoriSensori> valSenzori = GUIServerConnector.getInstance().getServerInterface().getSensorData(sessionId);
            ListViewItem momentCitire = new ListViewItem("Moment Citire:");
            ListViewItem valoareCitita = new ListViewItem("Valoare Citita:");
            // ListViewItem momentCitire = new ListViewItem(valSenzori.First().MementCitire.ToString());
         //   ListViewItem valoareCitita = new ListViewItem(valSenzori.First().ValoareCitita.ToString());
            //  nume.SubItems.Add("");
            listView.Items.Add(momentCitire);
            listView.Items.Add(valoareCitita);
            foreach (ValoriSensori valoare in valSenzori)
            {
                momentCitire.SubItems.Add(valSenzori.First().MementCitire.ToString());
                valoareCitita.SubItems.Add(valoare.ValoareCitita.ToString());
                // listView.Items.Add(nume);
            }

        }



        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Pacient> listaPacienti = GUIServerConnector.getInstance().getServerInterface().getPacientsData(sessionId);

            foreach(Pacient pacient in listaPacienti)
            {
                if (listView.SelectedItems.Count > 0)
                {
                    foreach (ListViewItem selected in listView.SelectedItems)
                    {
                        string numePrenume = pacient.Nume + " " + pacient.Prenume;
                        if (selected.Text.Equals(numePrenume))
                        {
                            LoadPacientData(pacient);
                            LoadTratamentData(pacient);
                            LoadSenzorsData();
                        }
                        
                    }
                }
            }
        }

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        { 
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listView_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView.Columns[e.ColumnIndex].Width;
        }

        private void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
        {
            e.Cancel = true;
            e.NewWidth = listView1.Columns[e.ColumnIndex].Width;
        }

    }
}
